package com.achun.ximalayamvvm.view.fragment

import android.content.Intent
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.achun.ximalayamvvm.R
import com.achun.ximalayamvvm.RecommendListDetailActivity
import com.achun.ximalayamvvm.databinding.FragmentSubscribeBinding
import com.achun.ximalayamvvm.utils.LogUtil
import com.achun.ximalayamvvm.utils.Uiloader
import com.achun.ximalayamvvm.view.adapter.RecommendListAdapter
import com.achun.ximalayamvvm.view.custom.CancelSubDialog
import com.achun.ximalayamvvm.vm.RecommendDetailViewModel
import com.ximalaya.ting.android.opensdk.model.album.Album
import java.util.*

class SubscribeFragment:Fragment() {
private var uiloader:Uiloader?=null
lateinit var sublistAdapter: RecommendListAdapter
private lateinit var subDialog:CancelSubDialog
    private lateinit var currentAlbum: Album


    @RequiresApi(Build.VERSION_CODES.P)
    private fun createSuccessView(inflater: LayoutInflater, container: ViewGroup): View? {
        var view =
            LayoutInflater.from(container.context).inflate(R.layout.sub_fragment, container, false)
        var subList = view.findViewById<RecyclerView>(R.id.sub_list_rv)
        subList.run {
            layoutManager=LinearLayoutManager(context)
            sublistAdapter= RecommendListAdapter()
            adapter=sublistAdapter
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    itemPosition: Int,
                    parent: RecyclerView
                ) {
                    outRect.right = 20
                    outRect.left = 20
                    outRect.top = 20
                    outRect.bottom = 20
                }
            })
        }

        return view
    }

    @RequiresApi(Build.VERSION_CODES.P)
    private fun initViewModel() {
        var recommendDetailViewModel = RecommendDetailViewModel.instance
        //加载一次数据
        LogUtil.e(this,"加载第一次数据")
        recommendDetailViewModel.loadSubsced()
        //观察列表数据
        recommendDetailViewModel.getSubAlbums().observeForever(){
            LogUtil.e(this,"加载数据了哇")
            uiloader?.updataStatus(Uiloader.UISTATUS.SUCCESS)
            var albums = it
            Collections.reverse(albums)
            sublistAdapter.setDatas(albums)
        }
        //初始化事件
        initEvent(recommendDetailViewModel)
    }

    private fun initEvent(recommendDetailViewModel: RecommendDetailViewModel) {
        //列表点击事件
        sublistAdapter.setOnclickItemListenter(object :RecommendListAdapter.OnClickItemListenter{
            override fun onclick(album: Album) {
                var instance = RecommendDetailViewModel.instance
                instance.setAlbums(album)
                instance.loadSubsced()
                startActivity(Intent(context, RecommendListDetailActivity::class.java))
            }

        })
        //长按删除事件
        sublistAdapter.setOnLongClickItemListenter(object :RecommendListAdapter.OnLongClickItemListenter{
            override fun onclick(album: Album) {
                currentAlbum=album
                subDialog.show()
            }
        })
        //弹窗点击事件
        subDialog.setOnsetDialogClickListenter(object :CancelSubDialog.OnsetDialogClickListenter{
            override fun onclickCancel() {
              subDialog.dismiss()
            }

            override fun onclickConfirm() {
               recommendDetailViewModel.deleteSub(currentAlbum)
                subDialog.dismiss()
            }
        })
    }

    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        uiloader= object :Uiloader(container!!.context){
            override fun retryOnclick() {
                TODO("Not yet implemented")
            }

            @RequiresApi(Build.VERSION_CODES.P)
            override fun getSuccessView(): View? {
                return createSuccessView(inflater,container!!)
            }
        }

        var subscribeBinding = DataBindingUtil.inflate<FragmentSubscribeBinding>(
            inflater,
            R.layout.fragment_subscribe,
            container,
            false
        )
        var root:FrameLayout = subscribeBinding.root as FrameLayout
        root.addView(uiloader)
        uiloader?.updataStatus(Uiloader.UISTATUS.LOADING)
        initView(container)
        initViewModel()



        return subscribeBinding.root
    }

    private fun initView(container: ViewGroup) {
        subDialog= CancelSubDialog(container.context)
    }
}